package com.example.controller;


import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.Result;
import com.example.entity.dto.*;
import com.example.entity.mysql.Resource;
import com.example.entity.yifei.CallCenterConfiguration;
import com.example.entity.yifei.Guzhanglog;
import com.example.entity.yifei.Mocty;
import com.example.enums.MachineStatusEnum;
import com.example.exception.CustomException;
import com.example.mapper.CallCenterConfigMapper;
import com.example.mapper.GuzhanglogMapper;
import com.example.mapper.MoctyMapper;
import com.example.mapper.pc.ResourceMapper;
import com.example.service.GuzhanglogService;
import com.example.untils.BeanUtils;
import com.example.untils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lan
 * @since 2024-11-25
 */
@RestController
@Api(tags = "机台故障")
@RequestMapping("/api/guzhanglog")
public class GuzhanglogController {
    @Autowired
    private GuzhanglogMapper guzhanglogMapper;
    @Autowired
    private GuzhanglogService guzhanglogService;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private MoctyMapper moctyMapper;
    @Autowired
    private CallCenterConfigMapper callCenterConfigMapper;

    @ApiOperation("新增故障单")
    @PostMapping("addGuZhangLog")
    @DSTransactional
    public Result addGuZhangLog(@RequestBody AddGuZhangLogDto dto) {
        if(StringUtils.isEmpty(dto.getMacno()) || StringUtils.isEmpty(dto.getGuZhangTypeNo()) || StringUtils.isEmpty(dto.getBanbie())) {
            throw new CustomException("500", "机器编号和故障类型和班别不能为空.");
        }
        List<Guzhanglog> guzhanglogList = guzhanglogMapper.selGuZhangByMacNoAndGuZhangTypeNo(dto.getMacno(), dto.getGuZhangTypeNo());
        if(guzhanglogList != null && guzhanglogList.size() > 0) {
            throw new CustomException("500", "当前机器:" + dto.getMacno().trim() + "上已有故障类型为:" + dto.getGuZhangTypeName().trim() +"未完成的故障单！");
        }
        Resource resource = resourceMapper.selectOne(new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, dto.getMacno()));
        if(resource == null) {
            throw new CustomException("500", "机器编号" + dto.getMacno() + "不存在.");
        }
        String oldStatus = resource.getStatus();
        resource.setStatus(MachineStatusEnum.GuZhan.getValue());
        resourceMapper.update(resource, new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, dto.getMacno()));
        Mocty mocty = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, dto.getMoctyudf05()));
        if(mocty == null) {
            throw new CustomException("500", "生产任务ID不存在");
        }
//        mocty.setUdf01(MOCStatusEnum.ZanTing.getValue());
//        moctyMapper.update(mocty, new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, dto.getMoctyudf05()));

        CallCenterConfiguration configuration = callCenterConfigMapper.getMachineConfig(dto.getMacno(), dto.getBanbie(), dto.getGuZhangTypeNo());
        if(configuration == null) {
            throw new CustomException("500", "机器在" + dto.getBanbie() + "班，没有绑定" + dto.getGuZhangTypeName() + "的配置。");
        }
        // 班别 + 类型 + 机器 要唯一。
        Guzhanglog log = new Guzhanglog();
        BeanUtils.copyPropertiesIgnoreCase(dto, log);
//         TODO 故障单上要绑定配置的故障类型
        log.setCallCenterConfigurationUuid(configuration.getUuid());
        log.setMacBeforeStatus(oldStatus);
        log.setIsFinish("N");
        guzhanglogMapper.insert(log);

        // 先通知第一层人
        guzhanglogService.tongZhiNextPeople(log.getId(), "1");

        return Result.success();
    }

    @ApiOperation("保存故障单结束值(故障解除)")
    @PostMapping("saveGuZhangLogResult")
    @DSTransactional
    public Result saveGuZhangLogResult(@RequestBody SaveGuZhangLogResultDto dto) {
        Guzhanglog guzhanglog = guzhanglogMapper.selectById(dto.getId());
        if(guzhanglog == null) {
            throw new CustomException("500", "故障ID不存在");
        }
        BeanUtils.copyPropertiesIgnoreCase(dto, guzhanglog);
        guzhanglog.setIsFinish("Y");
        guzhanglogMapper.updateById(guzhanglog);

        Resource resource = resourceMapper.selectOne(new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, guzhanglog.getMacno()));
        if(resource == null) {
            throw new CustomException("500", "机器编号" + guzhanglog.getMacno() + "不存在.");
        }
        resource.setStatus(guzhanglog.getMacBeforeStatus());
        resourceMapper.update(resource, new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, guzhanglog.getMacno()));
        Mocty mocty = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, guzhanglog.getMoctyudf05()));
        if(mocty == null) {
            throw new CustomException("500", "生产任务ID不存在");
        }
//        mocty.setUdf01(guzhanglog.getMocBeforeStatus());
//        moctyMapper.update(mocty, new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, guzhanglog.getMoctyudf05()));

        return Result.success();
    }

    @ApiOperation("故障响应")
    @PostMapping("guZhangXiangYing")
    @DSTransactional
    public Result guZhangXiangYing(@RequestBody GuZhangXiangYingDto dto) {
        Guzhanglog guzhanglog = guzhanglogMapper.selectById(dto.getId());
        if(guzhanglog == null) {
            throw new CustomException("500", "故障ID不存在");
        }
        BeanUtils.copyPropertiesIgnoreCase(dto, guzhanglog);
        guzhanglog.setIsFinish("X");
        guzhanglogMapper.updateById(guzhanglog);
        return Result.success();
    }

    @ApiOperation("查找在线故障单(根据机器编号)")
    @PostMapping("selGuZhangLogByMacNo")
    public Result<List<Guzhanglog>> selGuZhangLogByMacNo(@RequestBody SelGuZhangLogByMacNoDto dto) {
        List<Guzhanglog> list = guzhanglogMapper.selByMacNo(dto.getMacno());
        return Result.success(list);
    }

    @ApiOperation("查找所有的在线故障单")
    @PostMapping("selGuZhangLogByMacNoAll")
    public Result<List<Guzhanglog>> selGuZhangLogByMacNoAll(@RequestBody SelGuZhangLogByMacNoDto dto) {
        List<Guzhanglog> list = guzhanglogMapper.selByMacNoAll(dto.getMacno());
        return Result.success(list);
    }

    @ApiOperation("查找机器故障单历史记录")
    @PostMapping("selGuZhangLogHisByMacNo")
    public Result<PageInfo<List<Guzhanglog>>> selGuZhangLogHisByMacNo(@RequestBody SelGuZhangLogHisByMacNoDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), "startTime DESC");
        List<Guzhanglog> list = guzhanglogMapper.selGuZhangLogHis(dto);
        PageInfo<List<Guzhanglog>> pageInfo = new PageInfo(list);
        return Result.success(pageInfo);
    }

}
